'use strict';

module.exports = app => {
    const { STRING } = app.Sequelize;

    const User = app.model.define('user', {
        id: { type: STRING, primaryKey: true, autoIncrement: true },
        name: STRING(255),
        sex: STRING(32),
    });

    User.createUser = async function (obj) {
        console.log(obj)
        let me = this;
        me.create(obj);
    }

    User.deleteUser = async function (id) {
        let me = this;
        me.destroy({
            where: {
                id: id
            }
        })
    };


    User.updateUser = async function (obj) {
        let me = this;
        me.update(obj, {
            where: {
                id: obj.id
            }

        });
    };
    User.associate = async function () {
        //User.hasOne(app.model.Project, { foreignKey: 'user_id', sourceKey: 'id' }); //一对一查询
        //User.hasMany(app.model.Project, { foreignKey: 'user_id', sourceKey: 'id' }); //1对多查询
        //User.belongsTo(app.model.Project, { foreignKey: 'id', targetKey: 'user_id' });//一对一查询
        User.belongsToMany(app.model.Project, {//多对多
            // as: {
            //     singular: 'info',
            //     plural: 'info'
            // },
            through: 'user_project', foreignKey: 'user_id'
        })

        let me = this;
        return me.findAll({
            include: [{
                model: app.model.Project,
                required: false,
                // as: 'info'
            }]
        })
    };





    return User;
}